package com.dingreading.cloud.subject.service.impl;

import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.subject.dto.SubjectLessonDto;
import com.dingreading.cloud.subject.dto.SubjectLessonListDto;
import com.dingreading.cloud.subject.dto.SubjectLessonVideoDto;
import com.dingreading.cloud.subject.dto.SubjectLessonVideoListDto;
import com.dingreading.cloud.subject.entity.SubjectLesson;
import com.dingreading.cloud.subject.entity.table.SubjectLessonTableDef;
import com.dingreading.cloud.subject.mapper.SubjectLessonMapper;
import com.dingreading.cloud.subject.service.SubjectLessonService;
import com.dingreading.cloud.subject.util.CdnUrlUtil;
import com.mybatisflex.core.query.QueryColumn;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryMethods;
import com.mybatisflex.core.query.QueryWrapper;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2023-11-22
 */
@Service
public class SubjectLessonServiceImpl extends BaseServiceImpl<SubjectLessonMapper, SubjectLesson, SubjectLessonTableDef> implements SubjectLessonService {

    @Override
    protected SubjectLessonTableDef getTable() {
        return SubjectLessonTableDef.subjectLesson;
    }

    @Resource
    private CdnUrlUtil cdnUrlUtil;


    @Override
    public List<SubjectLessonListDto> getSubjectLessonNoUrl(Long subjectId) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.subjectId.eq(subjectId))
                .and(table.enabled.eq(1))
                .and(table.lessonName.isNotNull());

        QueryWrapper wrapper = QueryWrapper.create()
                .select(getLessonNoUrl())
                .from(table)
                .where(condition)
                .orderBy(table.lessonOrder.asc());

        return getMapper().selectListWithRelationsByQueryAs(wrapper, SubjectLessonListDto.class);
    }

    @Override
    public SubjectLessonDto getDtoById(Long id) {
        QueryWrapper wrapper = QueryWrapper.create()
                .where(table.id.eq(id));
        return getMapper().selectOneWithRelationsByQueryAs(wrapper, SubjectLessonDto.class);
    }

    @Override
    public SubjectLessonDto dtoRelationByUid(String uid) {
        QueryWrapper wrapper = QueryWrapper.create()
                .where(table.uid.eq(uid));
        return getMapper().selectOneWithRelationsByQueryAs(wrapper, SubjectLessonDto.class);
    }

    @Override
    public SubjectLessonDto dtoByUid(String uid) {
        QueryWrapper wrapper = QueryWrapper.create()
                .where(table.uid.eq(uid));
        return getMapper().selectOneByQueryAs(wrapper, SubjectLessonDto.class);
    }

    private QueryColumn[] getLessonNoUrl() {
        QueryColumn queryColumn = QueryMethods.if_(table.dataUrl.isNotNull(), QueryMethods.true_(), QueryMethods.false_());

        return new QueryColumn[]{
                table.id, table.lessonOrder, table.lessonName, table.teachAims,
                table.subjectMainId, table.subjectMainName, table.subjectId, table.dataUrl,
                table.uid, table.bookImg, table.bookIsbn, table.bookPress,
                queryColumn.as("downloadStatus")};
    }

    private QueryColumn[] getLesson() {
        return new QueryColumn[]{
                table.id, table.lessonOrder, table.lessonName, table.enabled, table.teachAims,
                table.subjectMainId, table.subjectMainName, table.subjectId, table.htmlUrl,
                table.teachPlanUrl, table.dataUrl, table.dxhLessonId,
                table.uid, table.bookImg, table.bookIsbn, table.bookPress,
        };
    }

    @Override
    public SubjectLessonVideoListDto getVideoList(String uid) {
        QueryWrapper wrapper = QueryWrapper.create()
                .where(table.uid.eq(uid));
        SubjectLessonVideoListDto subjectLessonVideoListDto = getMapper().selectOneWithRelationsByQueryAs(wrapper, SubjectLessonVideoListDto.class);
        if (subjectLessonVideoListDto != null) {
            List<SubjectLessonVideoDto> videoList = subjectLessonVideoListDto.getVideoList();
            if (CollectionUtils.isNotEmpty(videoList)) {
                List<SubjectLessonVideoDto> filteredVideoList = videoList.stream()
                        .filter(video -> video.getVideoType() != null && video.getVideoType() == 0)
                        .collect(Collectors.toList());
                subjectLessonVideoListDto.setVideoList(filteredVideoList);
            }
        }
        return subjectLessonVideoListDto;
    }

    @Override
    public R<Object> getTeachVideoList(String uid) {
        QueryWrapper wrapper = QueryWrapper.create()
                .where(table.uid.eq(uid));
        SubjectLessonVideoListDto subjectLessonVideoListDto = getMapper().selectOneWithRelationsByQueryAs(wrapper, SubjectLessonVideoListDto.class);
        if (subjectLessonVideoListDto != null) {
            List<SubjectLessonVideoDto> videoList = subjectLessonVideoListDto.getVideoList();
            if (CollectionUtils.isNotEmpty(videoList)) {
                List<SubjectLessonVideoDto> filteredVideoList = videoList.stream()
                        .filter(video -> video.getVideoType() != null && video.getVideoType() == 1)
                        .collect(Collectors.toList());
                subjectLessonVideoListDto.setVideoList(filteredVideoList);
            }
        }
        return R.ok(subjectLessonVideoListDto);
    }

    @Override
    public R<Object> getDouXueHuiVideoList(
            HttpServletRequest request, String corpUid, String storeUid, String storeName, String staffUid, String staffName, Long id
    ) {
        QueryCondition condition = QueryCondition.createEmpty()
                .and(table.id.eq(id));
        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition);
        SubjectLessonVideoListDto subjectLessonVideoListDto = getMapper().selectOneWithRelationsByQueryAs(wrapper, SubjectLessonVideoListDto.class);
        if (subjectLessonVideoListDto != null) {
            List<SubjectLessonVideoDto> videoList = subjectLessonVideoListDto.getVideoList();
            if (CollectionUtils.isNotEmpty(videoList)) {
                List<SubjectLessonVideoDto> filteredVideoList = videoList.stream()
                        .filter(video -> video.getVideoType() != null && video.getVideoType() == 1)
                        .collect(Collectors.toList());
                for (SubjectLessonVideoDto video : filteredVideoList) {
                    String originalUrl = video.getUrl();
                    if (StringUtils.isNotBlank(originalUrl)) {
                        R<String> r = cdnUrlUtil.getCdnUrl(request, originalUrl);
                        if (R.isError(r)) return R.fail(r.getMsg());
                        String cdnUrl = r.getData();
                        video.setUrl(cdnUrl);
                    }
                }
                return R.ok(filteredVideoList);
            }
        }
        return R.ok();
    }

}
